package seat_service

import (
	"zhanyan/models"
	"zhanyan/pac/util"
)

// Seat 座位
type Seat struct {
	ID          int `json:"id";gorm:"primary_key"`
	X           float64
	Y           float64
	StudentID   int `json:"student_id"`
	ClassroomID int `json:"classroom_id"`
}

func (seat *Seat) AddOnClassroom() error {
	_, err2 := models.CheckClassroomByID(seat.ClassroomID)
	if err2 != nil {
		return util.ClassroomNotExistError
	}
	s := models.Seat{}
	err := util.Transfer(seat, &s)
	if err != nil {
		return err
	}
	seat.ID, err = models.AddClassroomSeat(&s)
	return nil
}

func (seat *Seat) AddOnStudent() error {
	_, err := models.CheckStudentById(seat.StudentID)
	if err != nil {
		return util.UserNotExistError
	}
	checkStudentSeat, _ := models.CheckStudentSeat(seat.StudentID)
	if checkStudentSeat {
		return util.StudentHasSeatError
	}
	s, err := models.QuerySeat(seat.ID)
	if err != nil {
		return err
	}
	s.StudentID = seat.StudentID
	seat.ID, err = models.AddStudentSeat(s)
	return nil
}

// StudentIsBind 已绑定返回ture，未绑定false
func (seat *Seat) StudentIsBind() (bool, error) {
	_, err := models.CheckStudentById(seat.StudentID)
	if err != nil {
		return false, util.UserNotExistError
	}
	checkStudentSeat, _ := models.CheckStudentSeat(seat.StudentID)
	if checkStudentSeat {
		return true, nil
	}
	return false, err
}

func (seat *Seat) UpdateOnStudent() error {
	_, err := models.CheckStudentById(seat.StudentID)
	if err != nil {
		return util.UserNotExistError
	}
	checkStudentSeat, _ := models.CheckStudentSeat(seat.StudentID)
	if !checkStudentSeat {
		return util.StudentNotHasSeatError
	}
	s, err := models.QuerySeat(seat.ID)
	if err != nil {
		return err
	}
	s.StudentID = seat.StudentID
	err = models.UpdateStudentSeat(s)
	if err != nil {
		return err
	}
	return nil
}

func (seat *Seat) DeleteOnStudent() error {
	_, err := models.CheckStudentById(seat.StudentID)
	if err != nil {
		return util.UserNotExistError
	}
	checkStudentSeat, _ := models.CheckStudentSeat(seat.StudentID)
	if !checkStudentSeat {
		return util.StudentNotHasSeatError
	}
	s := &models.Seat{}
	err = util.Transfer(seat, s)
	if err != nil {
		return err
	}
	err = models.DeleteSeatOnStudent(s)
	if err != nil {
		return err
	}
	return nil
}

func (seat *Seat) DeleteSeatOnClassroom() error {
	_, err := models.CheckClassroomByID(seat.ClassroomID)
	if err != nil {
		return util.ClassroomNotExistError
	}
	s := &models.Seat{}
	err = util.Transfer(seat, s)
	if err != nil {
		return err
	}
	seats, err := models.QuerySeats(seat.ClassroomID)
	if err != nil {
		return err
	}
	ids := []int{}
	for _, s2 := range *seats {
		ids = append(ids, s2.ID)
	}
	err = models.DeleteSeatOnClass(s)
	if err != nil {
		return err
	}
	err = models.DeleteSeats(ids)
	if err != nil {
		return err
	}
	return nil
}

func (seat *Seat) DeleteOneSeat() error {
	_, err := models.CheckClassroomByID(seat.ClassroomID)
	if err != nil {
		return util.ClassroomNotExistError
	}
	err = models.DeleteSeats([]int{seat.ID})
	if err != nil {
		return err
	}
	return nil
}

// GetSeats 根据教室id获取座位
func (seat *Seat) GetSeats() (*[]models.Seat, error) {
	_, err := models.CheckClassroomByID(seat.ClassroomID)
	if err != nil {
		return nil, util.ClassroomNotExistError
	}
	seats, err := models.QuerySeats(seat.ClassroomID)
	if err != nil {
		return nil, util.SeatNotFoundError
	}
	return seats, nil
}
